home *** CD-ROM | disk | FTP | other *** search
/ Ahoy 1987 July / Ahoy_Magazine_87-07_1987_Double_L_Side_B.d64 / comal article < prev    next >
Text File  |  2022-10-26  |  8KB  |  291 lines

  1. COMAL - THE NEW PROGRAMMING LANGUAGE
  2.  
  3.            by David Stidolph
  4.  
  5. A little over 10 years ago, Borge
  6. Christensen, a professor in Denmark,
  7. couldn't find a computer language he
  8. liked enough to teach.
  9.  
  10. BASIC was fine for the students - it
  11. was interactive and they could start
  12. learning quickly because they got to
  13. see their program execute as soon as
  14. they typed RUN. Unfortunately the
  15. student programs looked like
  16. spaghetti and it took longer to
  17. figure out how they worked than it
  18. took to write them.
  19.  
  20. Pascal, on the other hand, was a
  21. teachers dream. Programs were
  22. readable and precise. The long
  23. variable names and procedures and
  24. functions made checking the programs
  25. easy when compared to BASIC. The
  26. problem with Pascal, however, was
  27. that the students had to learn how to
  28. run the compiler before they could
  29. run a single line of code. The
  30. students also found it harder to
  31. experiment with Pascal than with
  32. BASIC. Every variable had to be
  33. declared and a student would never
  34. know about errors until the program
  35. was compiled.
  36.  
  37. Rather than choosing one or the
  38. other, Borge decided to develop a
  39. language that would include the best
  40. features of both BASIC and Pascal.
  41. This language was called COMAL
  42. (COMmon Algorithmic Language). His
  43. idea was so successful that it has
  44. become the official programing
  45. language of schools in five European
  46. countries. A COMAL Standard was
  47. developed (called the COMAL Kernal),
  48. and meetings are held annually to
  49. maintain compatibility between the
  50. various implementations.
  51.  
  52. Technically COMAL is a three pass,
  53. run time compiler. It combines the
  54. features of a compiler and an
  55. interpreter, providing an
  56. easy-to-learn language that also is
  57. easy-to-use. Explaining the three
  58. passes will help describe the
  59. language.
  60.  
  61. Pass One - Entering Program Lines
  62.  
  63. The first pass occurs when a program
  64. line is entered. The line's syntax is
  65. immediately checked. If there is any
  66. problem, an error message is printed
  67. below the line and the cursor is
  68. placed on the problem. For example,
  69. look at the following: 100
  70. hypotenuse=sqr(sidea+sideb(
  71.  
  72. The open parentheses at the end of
  73. the line should be a close
  74. parentheses. COMAL realizes this and
  75. displays the message ")" expected,
  76. not "(" beneath the line as soon as
  77. RETURN is pressed. The cursor is
  78. placed on the open parentheses at the
  79. end of the line.
  80.  
  81. To correct the error, press the )
  82. key. The line on the screen is now
  83. correct. Press the RETURN key and the
  84. line is entered into the program.
  85. Also, the error message is removed
  86. and the text it erased is restored.
  87. The command: LIST 100 displays the
  88. following line:
  89.  
  90. 0100 hypotenuse:=SQR(sidea+sideb)
  91.  
  92. Notice that COMAL distinguishes
  93. between := for assignment and = for
  94. comparison, but doesn't require you
  95. to type them differently. SQR is
  96. capitalized to show that it is a
  97. built in keyword of COMAL. Variables
  98. and commands can be entered in either
  99. upper or lower case.
  100.  
  101. Consider the following line:
  102.  
  103. 100 balance=12376.35
  104.  
  105. This line is valid in both COMAL and
  106. BASIC. However, in BASIC, the line is
  107. stored in its ASCII form, and only
  108. the first two characters of the
  109. variable name are significant
  110. (balance would be the same variable
  111. as bad, bank, bankrupt and backpay).
  112. Then, each time the line is executed,
  113. BASIC must convert the number to
  114. floating point format and search
  115. through memory for the variable ba.
  116.  
  117. In COMAL the variable name can be up
  118. to 78 characters long (each character
  119. is significant). To save programming
  120. space and increase execution speed,
  121. the name is stored in a table and a
  122. token is used at each occurrence in
  123. the program. When the program is
  124. executing, COMAL knows precisely
  125. where to find the variable. In
  126. addition, the number is stored in its
  127. floating point form, allowing faster
  128. execution. 
  129.  
  130. Pass Two - Program Structure Check
  131.  
  132. When you type RUN and press RETURN,
  133. COMAL checks the entire program for
  134. structural errors and resolves all
  135. addressing (noting where procedures
  136. are located, where to branch for
  137. IF..THEN..ELSE, FOR..NEXT loops,
  138. etc). This scan of the program is not
  139. noticeable (usually less than a
  140. second). For example, if a REPEAT
  141. loop was ended with ENDWHILE instead
  142. of UNTIL, COMAL would give the
  143. following error message (xxxx is the
  144. problem line number):
  145.  
  146. at xxxx: "UNTIL" expected, not
  147. "ENDWHILE"
  148.  
  149. BASIC cannot find a structural error
  150. until it executes the line. In COMAL
  151. each line of the program is checked
  152. as it is entered and the whole
  153. program is checked before a single
  154. line is executed. This program scan
  155. greatly improves program reliability
  156. and allows COMAL programs to run much
  157. faster than BASIC.
  158.  
  159. Pass Three - Running the Program
  160.  
  161. The last pass is when the program is
  162. actually executed. Although the
  163. program has been checked thoroughly
  164. for errors and all necessary jump
  165. addresses have already been
  166. calculated, the possibility of error
  167. still exists. Look at the following
  168. program:
  169.  
  170. 0010 PAGE //clears screen
  171. 0020 INPUT "Apples?": apples
  172. 0030 INPUT "People?": people
  173. 0040 PRINT apples/people;
  174. 0050 PRINT "apples per person"
  175.  
  176. This simple program seems correct,
  177. but what if the user typed in a zero
  178. for people (a division by zero error)
  179. or typed a name instead of a number?
  180.  
  181. In most languages the program would
  182. halt. In fact, COMAL would halt in
  183. this case as well. There is a way,
  184. however, to trap errors. To "user
  185. proof" program lines, place them
  186. inside a TRAP .. HANDLER structure.
  187. This may sound complicated, but
  188. actually the idea is simple. Look at
  189. this re-worked example program
  190. (indentation is provided by the LIST
  191. command, you do not have to type the
  192. spaces): 
  193.  
  194. 0010 PAGE //clears screen
  195. 0012 LOOP
  196. 0015  TRAP
  197. 0020   INPUT "Apples?": apples
  198. 0030   INPUT "People?": people
  199. 0040   PRINT apples/people;
  200. 0050   PRINT "apples per person"
  201. 0060   EXIT //no errors
  202. 0070  HANDLER
  203. 0080   PRINT "Numbers only"
  204. 0090   PRINT "Please try again..."
  205. 0100  ENDTRAP
  206. 0110 ENDLOOP
  207.  
  208. Now if an error occurs during
  209. execution of lines between the TRAP
  210. and the HANDLER (lines 20 through
  211. 60), the program would jump into the
  212. HANDLER section (lines 80 - 90). If
  213. both input requests get proper
  214. replies, the EXIT command jumps out
  215. of the LOOP (and out of the TRAP as
  216. well). This is better than the
  217. AppleSoft ONERR command because it is
  218. a structure that can be put anywhere
  219. in the program without the worry of
  220. resetting any previous error trap.
  221.  
  222. Since lines were inserted between 10
  223. and 20, the RENUM command can be used
  224. to make all the line numbers nice and
  225. even. COMAL only uses line numbers
  226. only for editing programs and can
  227. even list a program without line
  228. numbers.
  229.  
  230. Portability of COMAL and Beyond
  231.  
  232. Thanks to the early development of a
  233. standard, a programmer can write a
  234. COMAL program that will run virtually
  235. unchanged on PC compatibles,
  236. Commodore 64, CP/M, and soon on the
  237. Apple II and Macintosh. With COMAL
  238. one language can be taught for
  239. several different machines. COMAL
  240. provides a universal language to
  241. bridge the gap.
  242.  
  243. Once you have mastered a given
  244. language, what do you do? No one
  245. language is perfect for all tasks.
  246. Each has its own advantages and
  247. disadvantages. The purpose of COMAL
  248. is to teach programming skills with a
  249. minimum of  "harassment". When you
  250. learn COMAL you have learned the
  251. standard programming techniques of
  252. Pascal, Modula II, Ada and other
  253. structured languages. Many of the
  254. concepts you learn in BASIC, LOGO,
  255. Forth, are actually undesirable and
  256. later must be "unlearned".
  257.  
  258. Other Features
  259.  
  260. COMAL has many other advantages - a
  261. full screen editor, strings and
  262. string arrays of any length (up to
  263. the limit of memory) with no garbage
  264. collection, fast string searches (up
  265. to 80 times faster than BASIC), print
  266. using, cursor control, procedures and
  267. functions with parameter passing and
  268. recursion, local/global variables,
  269. user defined string functions, and
  270. easy file access and control. COMAL
  271. also allows you to LIST procedures or
  272. entire programs to disk and later
  273. merge them into other programs. No
  274. more re-typing the same routine each
  275. time you need it in a program.
  276.  
  277. You now have COMAL 0.14. This is a
  278. powerful version of COMAL, but it
  279. does lack some Kernal commands (such
  280. as GET$, STR$ and VAL). A full COMAL
  281. implimentation is called COMAL 2.0
  282. and is available on cartridge for the
  283. c64/c128 and on CP/M machines. Please
  284. write for further information.
  285.  
  286. COMAL Users Group, U.S.A., Limited
  287. 6041 Monona Drive
  288. Madison, WI 53716
  289.  
  290. or call: (608) 222-4432
  291.